#include "inc/hw_types.h"
#include "drivers/display96x16x1.h"



void HelperDisplayInit()
{
    // Initialize the display driver and turn the display on.
    Display96x16x1Init(false);
    Display96x16x1DisplayOn();
}


void waitMiliSeconds(int miliSeconds)
{
	int ticksInAMiliSecond = 4545;

	for(int s = 0; s < miliSeconds; s++)
		for(int t = 0; t < ticksInAMiliSecond; t++);
}

void displayMessage(const char* title, const char* message)
{
	Display96x16x1Clear();
	Display96x16x1StringDraw(title, 0, 0);
	Display96x16x1StringDraw(message, 0, 1);
}

char* num2string(int number, char*string)
{
	char digit = 0;
	int divisor = 1000000000;

	// Convert the digits to characters
	for(int strIndex = 0; strIndex < 10; strIndex++)
	{
		digit = number / divisor;
		number = number % divisor;
		divisor = divisor / 10;

		string[strIndex] = '0' + digit;
	}

	// add the end of string delimiter
	string[10] = 0;

	// point to the first non zero character.
	for(int strIndex = 0; strIndex < 7; strIndex++)
	{
		if(string[0] == '0')
			string = string + 1;

		else
			break;
	}

	return string;
}

void displayAddress(unsigned char address[])
{
	// 123.567.901.345 0
	char addressString[16];
	char buffer[11];
	char* numStr;

	numStr = num2string((int)address[0], buffer);

	addressString[0] = numStr[0];
	addressString[1] = numStr[1];
	addressString[2] = numStr[2];
	addressString[3] = '.';

	numStr = num2string((int)address[1], buffer);

	addressString[4] = numStr[0];
	addressString[5] = numStr[1];
	addressString[6] = numStr[2];
	addressString[7] = '.';

	numStr = num2string((int)address[2], buffer);

	addressString[8] = numStr[0];
	addressString[9] = numStr[1];
	addressString[10] = numStr[2];
	addressString[11] = '.';

	numStr = num2string((int)address[3], buffer);

	addressString[12] = numStr[0];
	addressString[13] = numStr[1];
	addressString[14] = numStr[2];
	addressString[15] = 0;



	displayMessage("IPAddress", addressString);
}

void displayLineReading(char* message, char reading)
{
	// 1  1  1
	char sensorString[8];
	tBoolean sensor1 = reading & 1;
	tBoolean sensor2 = (reading & 2) >> 1;
	tBoolean sensor3 = (reading & 4) >> 2;


	sensorString[0] = '0' + sensor3;
	sensorString[1] = ' ';
	sensorString[2] = ' ';
	sensorString[3] = '0' + sensor2;
	sensorString[4] = ' ';
	sensorString[5] = ' ';
	sensorString[6] = '0' + sensor1;
	sensorString[7] = 0;


	Display96x16x1StringDrawCentered(message, 0, true);
	Display96x16x1StringDrawCentered(sensorString, 1, true);
}

void displayMessageAndNumber(const char* message, int number)
{
	char buffer1[11];
	char* number1String = num2string(number, buffer1);

	Display96x16x1Clear();
	Display96x16x1StringDraw(message, 0, 0);
	Display96x16x1StringDraw(&number1String[0], 0, 1);
}

void displayMessageAndNumbers(const char* message, int number, int number2)
{
	char buffer2[11];
	char* number2String = num2string(number2, buffer2);

	displayMessageAndNumber(message, number);
	Display96x16x1StringDraw(&number2String[0], 48, 1);
}

void displayMessagesAndNumber(const char* title, const char* message, int number)
{
	char buffer[11];
	char* numberString = num2string(number, buffer);

	displayMessage(title, message);
	Display96x16x1StringDraw(&numberString[0], 48, 1);
}
